package apis

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"vrcm/src/vrcm/app/admin/models/monitor"
	dto "vrcm/src/vrcm/app/admin/service/dto/monitor"
	service "vrcm/src/vrcm/app/admin/service/monitor"
	"vrcm/src/vrcm/common/apis"
)

type Worksite struct {
	apis.Api
}

func (e Worksite) GetPage(c *gin.Context) {
	req := dto.WorksiteGetPageReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	list := make([]monitor.Worksite, 0)
	var count int64

	err = s.GetPage(&req, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取Worksite失败，失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

func (e Worksite) Get(c *gin.Context) {
	req := dto.WorksiteGetReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.Get(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取Worksite失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "查询成功")
}

func (e Worksite) Insert(c *gin.Context) {
	req := dto.WorksiteInsertReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	b := s.IsExitWorksite(&dto.WorksiteUpdateReq{TenantId: req.TenantId, Worksite: req.Worksite})
	if b {
		e.Error(apis.WorksiteRepeatError.Code, err, fmt.Sprintf("创建工口失败，%s", "该工口已经被创建，请更换工口名"))
		return
	}
	err = s.Insert(&req)
	if err != nil {
		e.Error(apis.DBCreateError.Code, err, fmt.Sprintf("创建Worksite失败， %s", err.Error()))
		return
	}

	e.OK(req.Id, "创建成功")
}

func (e Worksite) Update(c *gin.Context) {
	req := dto.WorksiteUpdateReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	b := s.IsExitWorksite(&dto.WorksiteUpdateReq{TenantId: req.TenantId, Worksite: req.Worksite, Id: req.Id})
	if b {
		e.Error(apis.WorksiteRepeatError.Code, err, fmt.Sprintf("更新工口失败，%s", "该工口已经被创建，请更换工口名"))
		return
	}
	err = s.Update(&req)
	if err != nil {
		e.Error(apis.DBCreateError.Code, err, fmt.Sprintf("更新Worksite失败， %s", err.Error()))
		return
	}

	e.OK(req.Id, "更新成功")
}

func (e Worksite) Delete(c *gin.Context) {
	req := dto.WorksiteDeleteReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.Remove(&req)
	if err != nil {

		if err.Error() == string(apis.DeleteWorksiteError.Code) {
			e.Error(apis.DeleteWorksiteError.Code, err, fmt.Sprintf("删除Worksite失败， %s", "当前工口已被关联到成员，请解除关联后删除"))
		} else {
			e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("删除Worksite失败， %s", err.Error()))
		}
		return
	}
	e.OK(nil, "删除成功")
}

func (e Worksite) GetWorksiteMember(c *gin.Context) {
	req := dto.WorksiteMemberGetReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetWorksiteMember(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取member失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "查询成功")
}

func (e Worksite) GetMemberPage(c *gin.Context) {
	req := dto.MemberGetPageReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	list := make([]monitor.WorksiteMember, 0)
	var count int64

	err = s.GetMemberPage(&req, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取成员失败，失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

func (e Worksite) GetMember(c *gin.Context) {
	req := dto.MemberGetReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetMember(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取member失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "查询成功")
}

func (e Worksite) GetMemberByAuthority(c *gin.Context) {
	req := dto.MemberGetByAuthorityReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetMemberByAuthority(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取member失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "查询成功")
}

func (e Worksite) InsertMember(c *gin.Context) {
	req := dto.MemberInsertReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	b := s.IsExitWorksiteMember(&dto.MemberUpdateReq{TenantId: req.TenantId, MemberAccount: req.MemberAccount})
	if b {
		e.Error(apis.WorksiteMemberRepeatError.Code, err, fmt.Sprintf("创建成员失败，%s", "该账号已经被注册，请更换账号"))
		return
	}
	err = s.InsertMember(&req)
	if err != nil {
		e.Error(apis.DBCreateError.Code, err, fmt.Sprintf("创建成员失败， %s", err.Error()))
		return
	}

	e.OK(req.Id, "创建成功")
}

func (e Worksite) UpdateMember(c *gin.Context) {
	req := dto.MemberUpdateReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	b := s.IsExitWorksiteMember(&dto.MemberUpdateReq{TenantId: req.TenantId, MemberAccount: req.MemberAccount, Id: req.Id})
	if b {
		e.Error(apis.WorksiteMemberRepeatError.Code, err, fmt.Sprintf("创建成员失败， %s", "该账号已经被注册，请更换账号"))
		return
	}
	err = s.UpdateMember(&req)
	if err != nil {
		e.Error(apis.DBCreateError.Code, err, fmt.Sprintf("更新成员失败， %s", err.Error()))
		return
	}

	e.OK(req.Id, "更新成功")
}

func (e Worksite) RemoveMember(c *gin.Context) {
	req := dto.MemberDeleteReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.RemoveMember(&req)
	if err != nil {
		if err.Error() == string(apis.DeleteWorksiteMemberError.Code) {
			e.Error(apis.DeleteWorksiteMemberError.Code, err, fmt.Sprintf("删除成员失败， %s", "当前成员已被关联到活动，请解除关联后删除"))
		} else {
			e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("删除成员失败， %s", err.Error()))
		}

		return
	}
	e.OK(nil, "删除成功")
}

func (e Worksite) SaveWorksiteNotice(c *gin.Context) {
	req := dto.WorksiteNoticeInsertReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SaveWorksiteNotice(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("保存工口通知消息失败， %s", err.Error()))
		return
	}
	e.OK(nil, "保存工口通知消息成功")
}

func (e Worksite) UpdateWorksiteNotice(c *gin.Context) {
	req := dto.WorksiteNoticeUpdateReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.UpdateWorksiteNotice(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("保存工口通知消息失败， %s", err.Error()))
		return
	}
	e.OK(nil, "保存工口通知消息成功")
}

func (e Worksite) ExportWorksiteNotice(c *gin.Context) {
	req := dto.ExportWorksiteNoticeReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.ExportWorksiteNotice(&req)
	if err != nil {
		e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("导入工口通知消息失败， %s", err.Error()))
		return
	}
	e.OK(nil, "导入工口通知消息成功")
}

func (e Worksite) SetWorksiteNoticeDuration(c *gin.Context) {
	req := dto.SetWorksiteNoticeDurationReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SetWorksiteNoticeDuration(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("设置工口通知消息持续时间失败， %s", err.Error()))
		return
	}
	e.OK(nil, "设置工口通知消息持续时间成功")
}

func (e Worksite) DeleteWorksiteNotice(c *gin.Context) {
	req := dto.WorksiteNoticeDeleteReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.DeleteWorksiteNotice(&req)
	if err != nil {
		e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("删除工口通知消息失败， %s", err.Error()))
		return
	}
	e.OK(nil, "删除工口通知消息成功")
}

func (e Worksite) GetWorksiteNotice(c *gin.Context) {
	req := dto.WorksiteNoticeGetReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetWorksiteNotice(&req)
	if err != nil {
		e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("获取工口通通知息失败， %s", err.Error()))
		return
	}
	e.OK(data, "获取工口通知消息成功")
}

func (e Worksite) GetWorksiteNotices(c *gin.Context) {
	req := dto.WorksiteNoticeGetReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetWorksiteNotices(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("移动端获取工口通通知息失败， %s", err.Error()))
		return
	}
	e.OK(data, "移动端获取工口通知消息成功")
}

func (e Worksite) GetWorksiteNoticeHistory(c *gin.Context) {
	req := dto.WorksiteNoticeGetReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetWorksiteNoticeHistory(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("移动端获取发布工口通通知息失败， %s", err.Error()))
		return
	}
	e.OK(data, "移动端获取发布工口通知消息成功")
}

func (e Worksite) SendWorksiteNotice(c *gin.Context) {
	req := dto.WorksiteNoticeSendReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SendWorksiteNotice(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("发送工口通通知息失败， %s", err.Error()))
		return
	}
	e.OK(nil, "发送工口通知消息成功")
}

func (e Worksite) SetWorksiteAuthority(c *gin.Context) {
	req := dto.SetWorksiteAuthorityReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SetWorksiteAuthority(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("设置工口权限失败， %s", err.Error()))
		return
	}
	e.OK(nil, "设置工口权限成功")
}

func (e Worksite) SaveTaiBen(c *gin.Context) {
	req := dto.TaiBenInsertReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SaveTaiBen(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("设置台本失败， %s", err.Error()))
		return
	}
	e.OK(nil, "设置台本成功")
}

func (e Worksite) GetTaiBen(c *gin.Context) {
	req := dto.TaiBenGetReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetTaiBen(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取台本失败， %s", err.Error()))
		return
	}
	e.OK(data, "获取台本成功")
}

func (e Worksite) DeleteTaiBen(c *gin.Context) {
	req := dto.TaiBenDeleteReq{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.DeleteTaiBen(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("删除台本失败， %s", err.Error()))
		return
	}
	e.OK(nil, "删除台本成功")
}

func (e Worksite) NoticeConfirm(c *gin.Context) {
	req := dto.NoticeConfirm{}
	s := service.Worksite{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.NoticeConfirm(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("消息确认失败， %s", err.Error()))
		return
	}
	e.OK(nil, "消息确认成功")
}
